//
//  PersonnelListTableViewCell.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/9/14.
//

#import "PersonnelListTableViewCell.h"

@interface PersonnelListTableViewCell ()

@property (nonatomic ,strong) UIImageView * userImg;

@property (nonatomic ,strong) UILabel * realName;

@property (nonatomic ,strong) UILabel * phone;

@property (nonatomic ,strong) UILabel * postName;

@property (nonatomic ,strong) UILabel * count;

@property (nonatomic ,strong) UILabel * taskProgress;


@end

@implementation PersonnelListTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    UIView * makeview = [UIView new];
    makeview.backgroundColor = UIColor.whiteColor;
    [DTKTool YJ:kwt(5) andView:makeview];
    [self.contentView addSubview:makeview];
    [makeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.top.offset(kwt(5));
        make.right.offset(kwt(-10));
        make.bottom.offset(kwt(-5));
    }];
    
    UIImageView * userImg = kimageView(@"task_assigner_defaul_staff");
    self.userImg = userImg;
    [DTKTool YJ:kwt(20) andView:userImg];
    [makeview addSubview:userImg];
    [userImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.offset(kwt(16));
        make.height.width.offset(kwt(40));
    }];
    
    UILabel * realName = [DTKTool labelWithText:@"--" andFont:kCFont(19) andTextColor:KcolorHex(@"#222222")];
    self.realName = realName;
    [makeview addSubview:realName];
    [realName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(66));
        make.top.offset(kwt(15));
        make.height.offset(kwt(19));
    }];
    
    UIImageView * phoneimg = kimageView(@"employee_mobile_number");
    [makeview addSubview:phoneimg];
    [phoneimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(66));
        make.top.offset(kwt(45));
        make.height.width.offset(kwt(13));
    }];
    UILabel * phone = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    self.phone = phone;
    [makeview addSubview:phone];
    [phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(84));
        make.centerY.equalTo(phoneimg);
    }];
   
    UIImageView * phonerightimg = kimageView(@"list_return");
    [makeview addSubview:phonerightimg];
    [phonerightimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phone.mas_right).offset(kwt(4));
        make.centerY.equalTo(phoneimg);
        make.height.width.offset(kwt(10));
    }];
    MJWeakSelf

    phone.addClickBlock(^{
        [weakSelf openPhone];
    });
    phonerightimg.addClickBlock(^{
        [weakSelf openPhone];
    });
    UIView * addview = [UIView new];
    addview.backgroundColor = KcolorHex(@"#EEF4FF");
    [DTKTool YJ:kwt(5) andView:addview];
    [makeview addSubview:addview];
    [addview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.top.offset(kwt(21));
        make.height.offset(kwt(30));
        make.width.offset(kwt(86));
    }];
    addview.addClickBlock(^{
        [weakSelf openRW];
    });
    
    UILabel * addlab = [DTKTool labelWithText:@"+ 新增任务" andFont:kCFont(15) andTextColor:KcolorHex(@"#3D5FFF")];
    [addview addSubview:addlab];
    [addlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#EFEFEF");
    [makeview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(70));
        make.left.offset(kwt(12));
        make.right.offset(kwt(-12));
        make.height.offset(0.5);
    }];
    
    UILabel * postName = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    self.postName = postName;
    postName.numberOfLines = 0;
    [makeview addSubview:postName];
    [postName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.width.offset(kwt(325));
        make.top.offset(kwt(82));
    }];
    
    
    UIImageView * dbimg = kimageView(@"today_time");
    [makeview addSubview:dbimg];
    [dbimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.bottom.offset(kwt(-16));
        make.height.width.offset(kwt(15));
    }];
    
    
    UILabel * temp1 = [DTKTool labelWithText:@"今日待办:" andFont:kFont(15) andTextColor:KcolorHex(@"#787A79")];
    [makeview addSubview:temp1];
    [temp1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(34));
        make.centerY.equalTo(dbimg);
    }];
    UILabel * count = [DTKTool labelWithText:@"0" andFont:kCFont(18) andTextColor:KcolorHex(@"#FE0202")];
    self.count = count;
    [makeview addSubview:count];
    [count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(temp1.mas_right).offset(kwt(5));
        make.centerY.equalTo(temp1);
    }];
    
    
    UIImageView * wcimg = kimageView(@"completion_rate");
    [makeview addSubview:wcimg];
    [wcimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(165));
        make.bottom.offset(kwt(-16));
        make.height.width.offset(kwt(15));
    }];
    
    UILabel * temp2 = [DTKTool labelWithText:@"完成率:" andFont:kFont(15) andTextColor:KcolorHex(@"#787A79")];
    [makeview addSubview:temp2];
    [temp2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(188));
        make.centerY.equalTo(wcimg);
    }];
    UILabel * taskProgress = [DTKTool labelWithText:@"0" andFont:kCFont(18) andTextColor:KcolorHex(@"#121212")];
    self.taskProgress = taskProgress;
    [makeview addSubview:taskProgress];
    [taskProgress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(temp2.mas_right).offset(kwt(5));
        make.centerY.equalTo(temp2);
    }];
    
    
}
- (void)openPhone{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",self.model.phone]] options:nil completionHandler:nil];

}
- (void)openRW{
    if(self.openRWBlock){
        self.openRWBlock(self.model);
    }
}
- (void)setModel:(PersonnelModel *)model{
    _model = model;
    self.realName.text = model.realName;
    self.phone.text = model.phone;

    self.postName.attributedText = model.postNameStr;
    [self.postName mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(model.postNameStrH);
    }];
    self.count.text = [NSString stringWithFormat:@"%lu",model.incomplete];
    self.taskProgress.text = [NSString stringWithFormat:@"%lu%%",model.taskProgress];
    if(model.userIdentity == 1){
        self.userImg.image = [UIImage imageNamed:@"task_assigner_defaul_avatar"];
    } else if(model.userIdentity == 3){
        self.userImg.image = [UIImage imageNamed:@"task_assigner_defaul_staff"];

    }
}
@end
